home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Topik / Topik - Disk 18 - Serious (19xx)(Topik Public Domain)(PD)[WB].zip / Topik - Disk 18 - Serious (19xx)(Topik Public Domain)(PD)[WB].adf / BindNams / bindnames.c < prev    next >
C/C++ Source or Header  |  1989-10-01  |  12KB  |  364 lines

  1. /* ====================================================================== */ 
  2.  
  3. /*      BindNames.c by Dave Haynie 
  4.  
  5.         This is a simple utility to remove some of the drudgery of program 
  6.         installation.  Instead of having to edit a Startup-Sequence for 
  7.         every logical name that must be used by a new program, this lets 
  8.         names be block-allocated.  The BindNames program looks for files 
  9.         in the SYS:Names directory.  Each of these files contains any number 
  10.         of lines of the form: 
  11.         
  12.                 Name:   Path 
  13.         
  14.         Where "Name" is the logical name we're assigning, "Path" is the 
  15.         path name we're making equivalent.   
  16. */
  17.  
  18. #include <exec/types.h>
  19. #include <exec/memory.h>         
  20. #include <libraries/dos.h> 
  21. #include <libraries/dosextens.h> 
  22. #include <proto/all.h> 
  23. #include <stdio.h> 
  24. #include <ctype.h> 
  25. #include <stdlib.h> 
  26. #include <string.h> 
  27.  
  28. /* ====================================================================== */ 
  29.  
  30. /* Macros */
  31.  
  32. #define CADDR(x)        ((BPTR)(((ULONG)x)>>2))
  33. #define NAMEDIR         "SYS:Names" 
  34. #define CopyStr(s)      strcpy(malloc(strlen(s)+1),s) 
  35. #define FreeStr(s)      free(s) 
  36.  
  37. /* The "-rr" option doesn't like NewList, bit it's real simple.  WHEN can 
  38.    we have inline functions.... */ 
  39.  
  40. #define NewList(l)      { (l)->lh_Head     = (struct Node *)&(l)->lh_Tail; \ 
  41.                           (l)->lh_TailPred = (struct Node *)&(l)->lh_Head; \ 
  42.                           (l)->lh_Tail     = NULL;          \ 
  43.                         } 
  44.  
  45. /* ====================================================================== */ 
  46.  
  47. /* Global variables */
  48.  
  49. extern struct DosLibrary *DosBase = NULL; 
  50. struct DosInfo *info = NULL; 
  51. struct DeviceList *lastdev; 
  52. BPTR namelock = NULL; 
  53. BOOL verbose = FALSE; 
  54. BOOL test = FALSE; 
  55. struct FileInfoBlock *fileinfo = NULL; 
  56.  
  57. /* ====================================================================== */ 
  58.  
  59. /* This function makes a BCPL string into a C style string. */ 
  60.  
  61. char *b2cstr(char *cstr, BPTR bptr) { 
  62.    char *ptr = (char *)BADDR(bptr); 
  63.    strncpy(cstr,ptr+1,*ptr); 
  64.    cstr[*ptr] = '\0'; 
  65.    return cstr;          
  66. }
  67.  
  68. /* This function allocates things in a DOS friendly way. */ 
  69.  
  70. void *DOSAlloc(LONG size) {
  71.    LONG *ptr = AllocMem(size+4,MEMF_PUBLIC|MEMF_CLEAR); 
  72.    *ptr = size+4; 
  73.    return ptr+1;
  74. }
  75.  
  76. /* This function frees memory in a DOS friendly way. */ 
  77.  
  78. void DOSFree(void *mem) {
  79.    LONG *ptr = (LONG *)mem;
  80.    FreeMem(ptr-1,*(ptr-1)); 
  81. }
  82.  
  83. /* ====================================================================== */ 
  84.  
  85. /* Here we manage name trees.  Name nodes are built from the device list 
  86.    first.  All primary nodes based on the device list have NULL paths, 
  87.    indicating that they are primary and thus, don't need to be recreated. 
  88.    Subsequent nodes are added to the node that they depend on.  If a node  
  89.    is encountered that doesn't have a parent listed yet, it will be place 
  90.    on a temporary node list. */ 
  91.  
  92. struct NameNode {
  93.    struct Node node;
  94.    struct NameNode *parent; 
  95.    struct List children; 
  96.    char *path;
  97. };
  98.  
  99. struct List Names;
  100. struct NameNode *lostnodes;
  101.  
  102. /* This function creates a name node, allocating whatever memory is  
  103.    needed. */
  104.  
  105. struct NameNode *MakeNode(char *name, char *path) { 
  106.    struct NameNode *nn;
  107.    
  108.    if (!(nn = calloc(1,sizeof(struct NameNode)))) return NULL; 
  109.    if (name) nn->node.ln_Name = CopyStr(name); 
  110.    NewList(&nn->children); 
  111.    if (path) nn->path = CopyStr(path); 
  112.    return nn;
  113.  
  114. /* This function finds a particular name in the Names data base, via a  
  115.    depth-first search.  If it finds the name, it returns that node,  
  116.    otherwise, it returns NULL. */ 
  117.  
  118. struct NameNode *FindNode(struct List *lst,char *name) { 
  119.    struct Node *n; 
  120.    struct NameNode *nn; 
  121.    
  122.    for (n = lst->lh_Head; n->ln_Succ; n = n->ln_Succ) {
  123.       if (!(stricmp(name,n->ln_Name))) return (struct NameNode *)n; 
  124.       if (nn = FindNode(&((struct NameNode *)n)->children,name)) return nn; 
  125.    }
  126.    return NULL;
  127. }
  128.  
  129. /* This is the node assignment routine.  It accepts a node name and a path 
  130.    for assignment.  It handles all the proper node creations to add that 
  131.    information to the node data base. */ 
  132.    
  133. void DefNode(char *name, char *path) { 
  134.    struct NameNode *nn, *parent; 
  135.    char pardev[64]; 
  136.    int i;
  137.    
  138.    for (i = 0; path[i] != ':' && i < 63; ++i) pardev[i] = path[i]; 
  139.    pardev[i] = '\0'; 
  140.    if (!(parent = FindNode(&Names,pardev))) { 
  141.       AddHead(&lostnodes->children,(struct Node *)(parent=MakeNode(pardev,NULL))); 
  142.       parent->parent = lostnodes; 
  143.    }
  144.    
  145.    if (!(nn = FindNode(&Names,name)))
  146.       nn = MakeNode(name,path); 
  147.    else {
  148.       Remove((struct Node *)nn);
  149.       if (nn->path) FreeStr(nn->path); 
  150.       nn->path = CopyStr(path); 
  151.    }
  152.  
  153.    AddHead(&parent->children,(struct Node *)nn); 
  154. }  
  155.  
  156. /* This function reads in the individual file's data and builds entries
  157.    in the name list based on that data. */ 
  158.    
  159. void AddFIB(struct FileInfoBlock *fib,char *name) {   
  160.    BPTR file;
  161.    char *buf,*com,*path; 
  162.       
  163.    if (buf = AllocMem(fib->fib_Size+1,0L)) { 
  164.       if (file = Open(name,MODE_OLDFILE)) { 
  165.          Read(file,buf,fib->fib_Size); 
  166.          buf[fib->fib_Size] = '\0'; 
  167.          com = strtok(strupr(buf)," :\t\n"); 
  168.          path = strtok(NULL," \t\n"); 
  169.          while (com && path) { 
  170.             DefNode(com,path); 
  171.             com = strtok(NULL," :\t\n"); 
  172.             path = strtok(NULL," \t\n"); 
  173.          }
  174.          Close(file); 
  175.       }
  176.       FreeMem(buf,fib->fib_Size+1); 
  177.    }
  178. }
  179.  
  180. /* ====================================================================== */ 
  181.  
  182. /* This function opens up the stuff we need. */ 
  183.         
  184. BOOL DoInits(void) {
  185.    if (!(DosBase = (struct DosLibrary *)OpenLibrary("dos.library",33L))) 
  186.       return FALSE; 
  187.    if (!(info = (struct DosInfo *)BADDR(((struct RootNode *)DosBase->dl_Root)->rn_nfo)))
  188.       return FALSE;
  189.    if (!(fileinfo = AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC))) 
  190.       return FALSE; 
  191.               
  192.    return TRUE; 
  193. }
  194.  
  195. /* This function cleans up after DoInits. */ 
  196.  
  197. void CloseUp(int code, char *str) { 
  198.    if (str) printf("Error: %s\n",str); 
  199.    if (fileinfo) FreeMem(fileinfo,sizeof(struct FileInfoBlock)); 
  200.    if (namelock) UnLock(namelock); 
  201.    if (DosBase) CloseLibrary((struct Library *)DosBase); 
  202.    exit(code);    
  203. }
  204.  
  205. /* ====================================================================== */
  206.  
  207. /* This function finds a directory node.  It has the side effect of 
  208.    setting "lastdev" to the last device in the global device list. */ 
  209.  
  210. struct DeviceList *AllocDirNode(char *name) { 
  211.    struct DeviceList *dev, *newdev; 
  212.    char *str,old[64]; 
  213.  
  214.   /* Let's see if the assignment has already been made... */ 
  215.    for (dev = (struct DeviceList *)BADDR(info->di_DevInfo); dev != NULL;  
  216.         dev = (struct DeviceList *)BADDR(dev->dl_Next)) { 
  217.       lastdev = dev; 
  218.       if (dev->dl_Type != DLT_DIRECTORY) continue; 
  219.       b2cstr(old,dev->dl_Name); 
  220.       if (!stricmp(old,name)) return dev; 
  221.    }
  222.  
  223.   /* Create the new name structure */
  224.    if (!(newdev=DOSAlloc(sizeof(struct DeviceList))) || !(str=DOSAlloc(32L))) { 
  225.       if (newdev) DOSFree(newdev); 
  226.       return NULL; 
  227.    }
  228.    newdev->dl_Type = DLT_DIRECTORY; 
  229.    strcpy(str+1,name); 
  230.    str[0] = strlen(name); 
  231.    newdev->dl_Name = CADDR(str); 
  232.    return newdev;
  233. }
  234.  
  235. /* This function does the "Assign" operation. */ 
  236.  
  237. BOOL Assign(char *name, char *path) { 
  238.    BPTR block; 
  239.    struct DeviceList *newdev; 
  240.  
  241.   /* Let's verify that the path object is really there.  If not, try to make 
  242.      the thing. */ 
  243.    if (!(block = Lock(path,SHARED_LOCK))) { 
  244.       if (!(block = CreateDir(path))) return FALSE; 
  245.       UnLock(block); 
  246.       if (!(block = Lock(path,SHARED_LOCK))) return FALSE;
  247.    }
  248.  
  249.   /* Get a node for this assignment. */ 
  250.    Forbid();
  251.    newdev = AllocDirNode(name); 
  252.  
  253.   /* Make the assignment */
  254.    if (newdev->dl_Lock) { 
  255.       Permit(); 
  256.       UnLock(newdev->dl_Lock); 
  257.       newdev->dl_Lock = block;
  258.       newdev->dl_Task = ((struct FileLock *)BADDR(block))->fl_Task; 
  259.       return TRUE; 
  260.    }
  261.    newdev->dl_Lock = block;
  262.    newdev->dl_Task = ((struct FileLock *)BADDR(block))->fl_Task; 
  263.  
  264.    /* Now link it into the device list. */ 
  265.    newdev->dl_Next = lastdev->dl_Next; 
  266.    lastdev->dl_Next = CADDR(newdev); 
  267.    Permit();
  268.    return TRUE;
  269.  
  270. /* This list takes in a node list, and performs assignments for all non-primary 
  271.    nodes in the list, as long as "inhibit" is FALSE. */ 
  272.  
  273. void AssignList(struct List *lst, int level, BOOL inhibit) { 
  274.    struct Node *n; 
  275.    struct NameNode *nn; 
  276.    
  277.    for (n = lst->lh_Head; n->ln_Succ; n = n->ln_Succ) { 
  278.       nn = (struct NameNode *)n; 
  279.       if (nn->path) { 
  280.          if (!inhibit) Assign(nn->node.ln_Name,nn->path); 
  281.          if (verbose || inhibit) 
  282.          printf("%*s%-15s%*s%s\2330m\n",level+1,"\23333m",nn->node.ln_Name, 
  283.                                        14-level,"\23332m",nn->path); 
  284.       }
  285.       if (nn->children.lh_Head->ln_Succ)  
  286.          AssignList(&nn->children,level+2,inhibit); 
  287.    }
  288. }
  289.  
  290. /* ====================================================================== */ 
  291.  
  292. void main(int argc, char *argv[]) { 
  293.    char path[256]; 
  294.    long i;
  295.    struct NameNode *nn; 
  296.    struct DeviceList *dev; 
  297.  
  298.    if (!DoInits()) CloseUp(5,NULL); 
  299.  
  300.   /* Check the command line. */
  301.    for (i = 1; i < argc; ++i) 
  302.       switch (toupper(argv[i][0])) { 
  303.          case 'S':
  304.             Assign("SYS",argv[++i]); 
  305.             break; 
  306.          case 'V': 
  307.             verbose = TRUE; 
  308.             break; 
  309.          case 'T': 
  310.             test = TRUE; 
  311.             verbose = TRUE; 
  312.             break; 
  313.          case '?':
  314.             printf("\2337mBindNames V1.0 by Dave Haynie\2330m\n\n"); 
  315.             printf("Usage: %s [VERBOSE] [TEST] [SYSTEM drive]\n",argv[0]); 
  316.             CloseUp(0,NULL); 
  317.       }
  318.       if (verbose) printf("\2337mBindNames V1.0 by Dave Haynie\2330m\n\n"); 
  319.       
  320.   /* Let's build the internal device list.  We know this list is going to be 
  321.      a flat list; everything is primary, and thus hung from the root list. */ 
  322.    NewList(&Names); 
  323.    Forbid();
  324.    for (dev = (struct DeviceList *)BADDR(info->di_DevInfo); dev != NULL;  
  325.         dev = (struct DeviceList *)BADDR(dev->dl_Next)) { 
  326.       if (!(nn = MakeNode(strupr(b2cstr(path,dev->dl_Name)),NULL))) continue; 
  327.       AddHead(&Names,(struct Node *)nn); 
  328.    }
  329.    Permit();
  330.    
  331.   /* I need to build the "lostnodes" node.  In order to avoid name collisions, 
  332.      I make the name of this node '\0'.  We never need to search for it by 
  333.      name... */ 
  334.  
  335.    AddTail(&Names,(struct Node *)(lostnodes = MakeNode("\0",NULL))); 
  336.     
  337.   /* Here I build the list of required assignments by walking through the 
  338.      SYS:Names directory, and reading each file. */ 
  339.   
  340.     if ((namelock = Lock(NAMEDIR,SHARED_LOCK))) { 
  341.       Examine(namelock,fileinfo); 
  342.       while (ExNext(namelock,fileinfo) || IoErr() != ERROR_NO_MORE_ENTRIES) { 
  343.          if (fileinfo->fib_DirEntryType > 0) continue; 
  344.          strcat(strcat(strcpy(path,NAMEDIR),"/"),fileinfo->fib_FileName); 
  345.          AddFIB(fileinfo,path); 
  346.       }
  347.    }
  348.  
  349.   /* Now I've got everything; let's see what's actually here. */ 
  350.   
  351.    Remove((struct Node *)lostnodes); 
  352.  
  353.    if (verbose) printf("Assigned Names:\n"); 
  354.    AssignList(&Names,1,test); 
  355.    if (lostnodes->children.lh_Head->ln_Succ) { 
  356.       if (verbose) printf("\n"); 
  357.       printf("Warning: Can't Resolve Names:\n"); 
  358.       AssignList(&lostnodes->children,1,TRUE); 
  359.    }
  360.    CloseUp(0,NULL);
  361. }
  362.